(function (window, $, undefined) {
  'use strict';

  var dictData = {
    gender: {},
    source: {
      '1': 'iOS',
      '2': 'Android',
      '3': '小程序'
    },
    status: {
      '101': '正常',
      '102': '锁定',
      '103': '已删除'
    }
  };

  main(function () {

    var $tableCRM = $('#table-crm');
    util.jqGrid($tableCRM, {
      url: ajaxURL.ORGANIZATION + '/customer/queryCustomerList',
      // datatype: 'json',
      autowidth: true,
      height: $(window).height() - 320,
      multiselect: true,
      multiboxonly: true,
      shrinkToFit: false,
      colModel: [
        { name: 'kid', label: '序号', key: true, width: 50 },
        {
          label: '客户名称', name: 'name', width: 100, formatter: function (v, o, r) {
            return '<a href="javascript:;" class="view" data-id="' + r.kid + '">' + v + '</a>';
          }
        },
        { label: '手机号码', name: 'phone', width: 100 },
        { label: '性别', name: 'gender', width: 50, formatter: function (v, o, r) {
          return dictData.gender[v] || '';
        } },
        { label: '用户状态', name: 'status', width: 80, formatter: function (v, o, r) {
          return dictData.status[v] || '';
        } },
        { label: '注册时间', name: 'registerTime', width: 150, formatter: function (v, o, r) {
          return util.dateFormat(r.createTime);
        } },
        { label: '用户来源', name: 'source', width: 80, formatter: function (v) { return dictData.source[v] || '' } },
        { label: '总下单次数', name: 'count', width: 100 },
        { label: '最近下单时间', name: 'lastOrderDate', width: 150 },
        { label: '分享数', name: 'shareTimes', width: 100 },
        { label: '拉新用户数', name: 'pullNewUsers', width: 100 },
        { label: '总出行里程', name: 'totalMiles', width: 100 },
        { label: '总支付金额', name: 'paidAmount', width: 100 },
        { label: '总优惠金额', name: 'couponAmount', width: 100 }
      ],
      pager: '#pager-crm'
    });

    laydate.render({
      range: '至',
      elem: '#register-time'
    });

    util.getDicts([
      'gender'
    ]).then(function (datas) {
      $.each(datas[0], function (x, item) {
        dictData.gender[item.value] = item.name;
      });
      searchTable();
      $('#btn-search').click(searchTable);
    });

    // 搜索
    function searchTable() {
      var data = util.formData('#form-search');

      var searchData = {
        nameLike: data.nameLike,
        phoneLike: data.phoneLike
      };

      if (data.registerTime) {
        var temp = data.registerTime.split('至');
        searchData.createTimeBegin = util.getTime($.trim(temp[0] + ' 00:00:00'));
        searchData.createTimeEnd = util.getTime($.trim(temp[1] + ' 23:59:59'));
      }

      util.searchGrid($tableCRM, searchData);
      // console.log(data);
    }

    // 客户详情
    $tableCRM.on('click', '.view', function () {
      var $this = $(this);
      var id = $this.data('id');
      // 根据 id 请求数据

      var rowData = $tableCRM.jqGrid('getRowData', id);

      util.formHtmlData($('#layer-crm-detail'), rowData);

      util.layerOpen({
        title: false,
        content: $('#layer-crm-detail'),
        area: ['800px', '']
      });
    });

    // 订单记录的表格
    util.jqGrid('#table-order-record', {
      url: '/json/test.json',
      width: 960,
      height: 400,
      colModel: [
        { name: '_id', key: true, hidden: true },
        {
          label: '订单编号', name: 'orderId', width: 50
        },
        { label: '创建时间', name: 'createDate', width: 50 },
        {
          label: '城市', name: 'city', width: 50
        },
        { label: '服务类型', name: 'sType', width: 50 },
        { label: '用车类型', name: 'carType', width: 50 },
        { label: '上车地点', name: 'sAddr', width: 50 },
        { label: '下车地点', name: 'aAddr', width: 50 },
        { label: '订单状态', name: 'status', width: 50 },
        { label: '支付金额', name: 'money', width: 50 },
        { label: '支付方式', name: 'paidType', width: 50 },
        { label: '支付时间', name: 'paidTime', width: 50 }
      ],
      pager: '#pager-order-record'
    }, true);

    // 订单记录
    $('#btn-order-record').click(util.oneCheck($tableCRM, function (id, data) {
      util.layerOpen({
        title: false,
        content: $('#layer-order-record'),
        area: ['1000px', '']
      });
    }));

    // 拉新表格
    util.jqGrid('#table-pn', {
      url: '/json/test.json',
      width: 760,
      height: 400,
      colModel: [
        { name: '_id', key: true, hidden: true },
        {
          label: '关联用户ID', name: 'userId', width: 50
        },
        { label: '书记号码', name: 'phone', width: 50 },
        {
          label: '昵称', name: 'nickName', width: 50
        },
        { label: '总出行里程', name: 'sType', width: 50 },
        { label: '总支付金额', name: 'carType', width: 50 },
        { label: '用户状态', name: 'sAddr', width: 50 },
        { label: '注册时间', name: 'aAddr', width: 50 },
        { label: '客户来源', name: 'status', width: 50 }
      ],
      pager: '#pager-pn'
    }, true);

    // 拉新客户
    $('#btn-pn').click(util.oneCheck($tableCRM, function (id, data) {
      util.layerOpen({
        title: false,
        content: $('#layer-pn'),
        area: ['800px', '']
      });
    }));

    // 评价表格
    util.jqGrid('#table-comment', {
      url: '/json/test.json',
      width: 960,
      height: 400,
      colModel: [
        { name: '_id', key: true, hidden: true },
        {
          label: '订单编号', name: 'userId', width: 50
        },
        { label: '创建时间', name: 'phone', width: 50 },
        {
          label: '城市', name: 'nickName', width: 50
        },
        { label: '服务类型', name: 'sType', width: 50 },
        { label: '车辆类型', name: 'carType', width: 50 },
        { label: '车牌号', name: 'sAddr', width: 50 },
        { label: '司机ID', name: 'aAddr', width: 50 },
        { label: '司机号码', name: 'status', width: 50 },
        { label: '评价', name: 'status', width: 120 },
        { label: '评价时间', name: 'status', width: 50 }
      ],
      pager: '#pager-comment'
    }, true);

    // 评价
    $('#btn-comment').click(util.oneCheck($tableCRM, function (id, data) {
      util.layerOpen({
        title: false,
        content: $('#layer-comment'),
        area: ['1000px', '']
      });
    }));

    // 开票表格
    util.jqGrid('#table-invoice', {
      url: '/json/test.json',
      width: 960,
      height: 400,
      colModel: [
        { name: '_id', key: true, hidden: true },
        {
          label: '发票抬头', name: 'userId', width: 50
        },
        { label: '单位名称', name: 'phone', width: 120 },
        {
          label: '纳税人识别号', name: 'nickName', width: 90
        },
        { label: '发票内容', name: 'sType', width: 50 },
        { label: '发票全额', name: 'carType', width: 50 },
        { label: '关联订单数', name: 'sAddr', width: 50 },
        { label: '发票状态', name: 'aAddr', width: 50 },
        { label: '快递单号', name: 'status', width: 80 },
        { label: '提交开票时间', name: 'status', width: 70 }
      ],
      pager: '#pager-invoice'
    }, true);

    // 评价
    $('#btn-invoice').click(util.oneCheck($tableCRM, function (id, data) {
      util.layerOpen({
        title: false,
        content: $('#layer-invoice'),
        area: ['1000px', '']
      });
    }));

    // 导出
    $('#btn-export').click(function () {
      util.tip.warn('导出操作...');
    });




  });


})(window, jQuery);
